﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "ILineEditDefinition.h"
#include "ControlDefinition.h"
#include "ControlHandler.h"
#include "ILineEditEvent.h"
#include "GcmpGUiBaseImpl.h"

namespace gcmp
{
    static const int MAX_INT_VALUE = 65535;
    static const int MIN_INT_VALUE = -65535;

    class ControlDefinitionChangeEvent;
    class IControlDefinitionChangeEvent;
    class ILineEditEventArgs;

    using LineEditControlHandler = ControlHandler<ILineEditHandler, ILineEditEventArgs>;

    class GCMP_GUI_BASE_IMPL_EXPORT LineEditDefinition : public ILineEditDefinition
    {
    public:
        LineEditDefinition(const std::wstring& id, const std::wstring& caption, LineEditInputType inputType = LineEditInputType::InputString);
        
        LineEditDefinition(const std::wstring& id, const std::wstring& caption, double value, int decimals = 3);

        virtual ~LineEditDefinition();
        virtual LineEditInputType GetInputType() const override { return m_inputType; }

        virtual std::wstring GetText() const override { return m_text; };
        virtual bool SetText(const std::wstring& text) override;

        virtual bool IsEditing() const override { return m_isEditing; };
        virtual bool SetIsEditing(bool isEditing) override;

        virtual ILineEditEvent* GetEvent() const override { return const_cast<ILineEditEvent*>(m_pEvent.get()); }
        virtual void SelectAll() override;
        virtual void Clear() override;
        virtual std::wstring GetSelectedText() const override;
        virtual ControlDefinitionType GetType() const override { return ControlDefinitionType::LineEdit; }

        virtual bool SetValidator(OwnerPtr<ValidatorBase> opValidator) override;
        virtual ValidatorBase* GetValidator() override;

        IControlDefinitionChangeEvent* GetControlDefinitionChangeEvent() const;
        virtual int GetSelectionStart() const override;
        virtual void SetSelection(int start, int length) override;
    public:
        virtual std::wstring GetId() const override { return m_pCtrlItem->GetId(); }
        virtual std::wstring GetCaption() const override { return m_pCtrlItem->GetCaption(); }
        virtual std::wstring GetTooltip() const override { return m_pCtrlItem->GetTooltip(); }
        virtual bool IsVisible() const override;
        virtual bool IsEnabled() const override;
        virtual bool SetEnabled(bool enabled) override;
        virtual bool SetVisible(bool visible) override;
        virtual bool SetControlHandler(OwnerPtr<ILineEditHandler> pControlHandler) override;
        virtual bool BlockControlHandler(bool needBlock) override;
        virtual int GetDecimals() const override { return m_decimals; }
    private:
        bool IsDoubleValue(const std::wstring& str);
        bool IsIntValue(const std::wstring& str);

    private:
        OwnerPtr<ControlDefinition> m_pCtrlItem;
        LineEditControlHandler m_handler;
        LineEditInputType m_inputType;
        std::wstring m_text;
        int m_selectionStart;//选中的文字的第一个字符在lineedit文字的位置

        std::wstring m_selectedText;//选择的文字

        bool m_isEditing; // 是否正在编辑

        OwnerPtr<ILineEditEvent> m_pEvent;

        // 通知使用该定义的控件的事件
        OwnerPtr<ControlDefinitionChangeEvent> m_pDefinitionChangeEvent;

        OwnerPtr<ValidatorBase> m_opValidator;
        int m_decimals;
    };
}

